import store from '../store/index.js'

export const request = (params = {}, prefix = true) => {
	uni.showLoading({
		title: '请求加载中'
	})
	return new Promise((resolve, reject) => {
		wx.request({
			url: prefix ? (store.state.url + params.url) : params.url,
			method: params.method || 'GET',
			header: {
				// vuex中获取token
				'Authorization': store.state.token || '',
				// 设备缓存中获取token
				// 'Authorization': uni.setStorageSync('userToken') || ''
			},
			data: {...params.data},
			success: res => {
				resolve(res)
			},
			fail: err => {
				reject(err)
			},
			complete() {
				uni.hideLoading()
			},
			timeout: 10000
		})
	});
}
